package task;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

import org.dbunit.DatabaseUnitException;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.eclipse.swt.graphics.ImageData;


public class DatabaseImport extends DbTask {
	@Override
	protected void doRun(Map<String, String> props) throws SQLException {
		String schema = props.get("schema");
		String filename = props.get("filename");
		System.out.printf("Import dump file %s into schema: '%s'%n", filename, schema);

        try {
			IDatabaseConnection connection = new DatabaseConnection(conn(), schema);
			IDataSet dataSet = new FlatXmlDataSet(new FileInputStream(filename));
	        try
	        {
	            DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
	        }
	        finally
	        {
	            connection.close();
	        }
		} catch (DatabaseUnitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public String getDescription() {
		String desc = "Import a dump file into a database schema.";
		return desc;
	}

	@Override
	public ImageData getImageData() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String[] propertyNames() {
		String[] props = { "schema", "filename" };
		return props;
	}
	@Override
	public Object[] propertyTypes() {
		return new Object[] { null, "FILE" };
	}
}
